package VIP;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bookshopSM.BookshopGUI;

public class VIPSet extends JPanel{
    private BookshopGUI userInterface1,userInterface2;
    private JLabel b1,b2;
    private JButton  firstButton1,secondButton1, firstButton2,secondButton2;
    int vnoDelete;
    String names1[]={"青铜会员","白银会员","黄金会员","钻石会员"};
    static  Connection conn = null;
    private static String username ="root";
    private static String password="root" ;
    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3307/books_system";

    public VIPSet() throws SQLException{
        setLayout(new FlowLayout());
        userInterface1 = new BookshopGUI(names1 );
        userInterface2 = new BookshopGUI(names1 );
        b1=new JLabel("会员最低消费设置(元)      ");
        b1.setFont(new   java.awt.Font("楷体", 1, 20)); //字体，加粗，字号
        b2=new JLabel("折扣设置(0到1之间)      ");
        b2.setFont(new   java.awt.Font("楷体", 1, 20));
        Box box = Box.createVerticalBox();
        box.add(box.createVerticalStrut(15));
        box.add(b1);
        box.add(box.createVerticalStrut(15));
        box.add(userInterface1 );
        box.add(box.createVerticalStrut(20));
        box.add(b2);
        box.add(box.createVerticalStrut(15));
        box.add(userInterface2 );
        this.add(box);
        setVisible( true );

        firstButton1 = userInterface1.getDoTask1Button();
        firstButton1.setText( "确认" );
        firstButton1.addActionListener(
                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        try {
                            updateRecord1();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
        );

        secondButton1 = userInterface1.getDoTask2Button();
        secondButton1.setText( "清除" );
        secondButton1.addActionListener(

                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        userInterface1.clearFields();
                    }
                }
        );
        firstButton2 = userInterface2.getDoTask1Button();
        firstButton2.setText( "确认" );

        firstButton2.addActionListener(

                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        try {
                            updateRecord2();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }


                } // end anonymous inner class

        ); // end call to addActionListener

        secondButton2 = userInterface2.getDoTask2Button();
        secondButton2.setText( "清除" );
        secondButton2.addActionListener(

                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        userInterface2.clearFields();
                    }
                }
        );
        setSize( 500, 360 );
        setVisible( true );
    }

    private void updateRecord1() throws SQLException {
        // TODO Auto-generated method stub
        conn = DriverManager.getConnection(url,username,password);
        Statement stmt=conn.createStatement();
        String fieldValues1[] = userInterface1.getFieldValues();
        try {
            for(int i=0;i<4;i++){
                System.out.print(fieldValues1[i]);
                stmt.executeUpdate("update VIP set Vrank = '"+names1[i]+"'"+
                        " where Vconsume >="+Float.valueOf(fieldValues1[i]));
            }
            JOptionPane.showMessageDialog(null, "修改成功");

        }

        catch (Exception ee){
            JOptionPane.showMessageDialog(null, "无相关记录");
            System.out.println(ee);
        }
        finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn=null;
            }
        }

    }

    private void updateRecord2() throws SQLException {
        // TODO Auto-generated method stub
        conn = DriverManager.getConnection(url,username,password);
        Statement stmt=conn.createStatement();
        String fieldValues[] = userInterface2.getFieldValues();
        try {
            //"姓名","性别","联系方式","等级","总消费额","折扣额度","住址","备注"
            for(int i=0;i<4;i++){
                System.out.print(fieldValues[i]);
                stmt.executeUpdate("update VIP set Vdiscount="+Float.valueOf(fieldValues[i])+
                        " where Vrank='"+names1[i]+"'");
            }
            JOptionPane.showMessageDialog(null, "修改成功");
        }

        catch (Exception ee){
            System.out.println(ee);
        }
        finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn=null;
            }
        }

    }
}
